package 范围计算;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 算出中位数
 * @Author: SunAo
 * @Date: 2022/2/16 15:29
 */
public class 小米笔试题 {


    private List<Integer> list = new ArrayList();

    public void Insert(Integer num) {
        list.add(num);
        list = list.stream().sorted().collect(Collectors.toList());
    }

    public Double GetMedian() {
        if(list.size()<=1){
            return Double.valueOf(list.get(0));
        }
        int midIndex = (list.size()) / 2;
        if(list.size()%2 == 1){
            return Double.valueOf(list.get(midIndex));
        }else{
            Integer num1 = list.get(midIndex-1);
            Integer num2 = list.get(midIndex);
            return  Double.valueOf((num2 - num1) / 2.0f + num1);
        }
    }

    public static void main(String[] args) {
        小米笔试题 test = new 小米笔试题();
        test.list = test.list.stream().sorted().collect(Collectors.toList());
    }
}
